unit UFrmDict;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Buttons, UDM, USQLOperator, UIntfApp, IniFiles,
  ActnList, ComCtrls, ToolWin, ImgList, Spin, SynEdit, SynMemo, DB, uLkJSON, Common;

type
  TFrmDict = class(TFormEx)
    btnSave: TBitBtn;
    btn2: TBitBtn;
    shp1: TShape;
    lblDetail: TLabel;
    btnTableName: TBitBtn;
    actlst: TActionList;
    actSave1: TAction;
    pnl1: TPanel;
    grp1: TGroupBox;
    lstFieldList: TListBox;
    grp2: TGroupBox;
    lbl1: TLabel;
    lbl2: TLabel;
    edtFieldCode: TEdit;
    edtFieldName: TEdit;
    btnSave1: TBitBtn;
    lstCodeFilter: TListBox;
    lst1: TListBox;
    lbl3: TLabel;
    lbl4: TLabel;
    edtTableName: TEdit;
    lbl5: TLabel;
    edtTableCode: TEdit;
    procedure FormShow(Sender: TObject);
    procedure lstFieldListClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure btnTableNameClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure edtFieldNameKeyPress(Sender: TObject; var Key: Char);
    procedure actSave1Update(Sender: TObject);
    procedure lstCodeFilterClick(Sender: TObject);
    procedure actSave1Execute(Sender: TObject);
  private
    FJson: TlkJSONobject;
    NewAllField: TBaseTable;
    DataStat: TDataSetState;
    function SetDict(TableName, FieldName, FieldDisp: string): Boolean;
  public
    StrList: TStringList;
    sTableCode, sTableName: string;
    procedure GetFilterList(Code: string);
    procedure SetFieldNameFromCode(Code: string);
  end;

implementation

{$R *.dfm}

procedure TFrmDict.actSave1Execute(Sender: TObject);
var
  i: integer;
begin
  i := lstFieldList.ItemIndex;
  if i < 0 then
    Exit;
  NewAllField.Items[i].TableCode := trim(edtFieldCode.Text);
  NewAllField.Items[i].TableName := trim(edtFieldName.Text);
  NewAllField.Items[i].Selected := True;
  if lstFieldList.Items[i][1] <> '*' then
    lstFieldList.Items[i] := '*' + NewAllField.Items[i].TableCode + '(' + NewAllField.Items[i].TableName + ')'
  else
    lstFieldList.Items[i] := NewAllField.Items[i].TableCode + '(' + NewAllField.Items[i].TableName + ')';
  edtFieldName.Modified := false;
  actSave1.Enabled := False;
end;

procedure TFrmDict.actSave1Update(Sender: TObject);
begin
  if edtFieldName.Modified then
    actSave1.Enabled := True;
end;

function TFrmDict.SetDict(TableName, FieldName, FieldDisp: string): Boolean;
var
  i, j: Integer;
  jList: TlkJSONlist;
  jo: TlkJSONobject;
begin
  Result := False;
//  for i := 0 to AllTable.Count - 1 do
//  begin
//    if AllTable.Items[i].TableCode = TableName then
//    begin
//      jo := AllTable.Items[i].JSon;
//      if jo = nil then
//      begin
//        jo := TlkJSONobject.Create;
//        jo.Add('FrozenCols', 0);
//        jo.Add('TableName', '');
//        jo.Add('TableCode', TableName);
//        jo.Add('RowHeight', 25);
//        jList := TlkJSONlist.Create;
//        jo.Add('Columns', jList);
//      end;
//      if jo <> nil then
//      begin
//        jList := TlkJSONlist(Result.Field['Columns']);
//        if jlist <> nil then
//        begin
//          for j := 0 to jList.Count - 1 do
//          begin
//            jo := (jList.Child[j] as TlkJSONobject);
//            if jo.getString('field') = FieldCode then
//            begin
//              Result := jo;
//              Exit;
//            end;
//          end;
//        end;
//      end;
//    end;
//  end;
end;

procedure TFrmDict.btnSaveClick(Sender: TObject);
begin
  UDM.AllField.Assign(NewAllField);
  dmSQL.SaveAllField(sTableName, sTableCode);
  ModalResult := mrOk;
end;

procedure TFrmDict.SetFieldNameFromCode(Code: string);
var
  i: integer;
begin
  for I := 0 to lstCodeFilter.Count - 1 do
  begin
    if (UpperCase(lstCodeFilter.Items.Names[i]) = UpperCase(Code)) and
      (Trim(lstCodeFilter.Items.ValueFromIndex[i]) <> '') then
    begin
      edtFieldName.Text := lstCodeFilter.Items.ValueFromIndex[i];
      Break;
    end;
  end;
end;

procedure TFrmDict.btnTableNameClick(Sender: TObject);
var
  i: integer;
begin
 // i := BaseObjects.ItemIndex;
//  if SetObjectName(stTable, CurEnv.TableName, CurEnv.TableCode, nil) then
//  begin
//    if edtFieldName.CanFocus then
//      edtFieldName.SetFocus;
////    dmSQL.RefreshAllObject;
//  end;
end;

procedure TFrmDict.edtFieldNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    actSave1Execute(nil);
    lstFieldList.SetFocus;
    if lstFieldList.ItemIndex < lstFieldList.Count - 1 then
      lstFieldList.ItemIndex := lstFieldList.ItemIndex + 1 else
      lstFieldList.ItemIndex := 0;
    lstFieldList.OnClick(nil);
    edtFieldName.SetFocus;
  end;
end;

procedure TFrmDict.FormCreate(Sender: TObject);
begin
  NewAllField := TBaseTable.Create(TBaseTableItem);
end;

procedure TFrmDict.FormDestroy(Sender: TObject);
begin
  NewAllField.Free;
end;

procedure TFrmDict.FormShow(Sender: TObject);
var
  i,j: Integer;
  FieldName: string;
  jList: TlkJSONlist;
  jo: TlkJSONobject;
begin
  if Value1 = '' then
    Exit;
  edtTableCode.Text := Value1;
  for i := 0 to dmSQL.AllTable.Count - 1 do
  begin
    if dmSQL.AllTable.Items[i].TableCode = Value1 then
    begin
      jo := dmSQL.AllTable.Items[i].JSon;
      if jo = nil then
      begin
        jo := TlkJSONobject.Create;
        jo.Add('FrozenCols', 0);
        jo.Add('TableName', '');
        jo.Add('TableCode', VarToStr(Value1));
        jo.Add('RowHeight', 25);
        jList := TlkJSONlist.Create;
        jo.Add('Columns', jList);
      end;
      jList := TlkJSONlist(jo.Field['Columns']);
      if jlist <> nil then
      begin
        for j := 0 to jList.Count - 1 do
        begin
          jo := (jList.Child[j] as TlkJSONobject);
         // if jo.getString('field') = FieldCode then
         // begin
       //     Exit;
        //  end;
        end;
      end;
    end;
  end;
  Exit;
  NewAllField.Assign(UDM.AllField); //UDM.AllField.Count
 
    sTableCode := CurEnv.TableCode;
  sTableName := CurEnv.TableName;
  lstFieldList.Items.BeginUpdate;
  for i := 0 to NewAllField.Count - 1 do
    lstFieldList.Items.Add(NewAllField.Items[i].TableCode + '(' + NewAllField.Items[i].TableName + ')');
  lstFieldList.Items.EndUpdate;
  if lstFieldList.Items.Count > 0 then
  begin
    lstFieldList.ItemIndex := 0;
    lstFieldList.OnClick(nil);
    edtFieldName.SetFocus;
  end;
end;

procedure TFrmDict.GetFilterList(Code: string);
var
  i: Integer;
  aList: TStringList;
begin
  lstCodeFilter.Clear;
  aList := TStringList.Create;
  try
    dmSQL.IniFile.GetStrings(aList);
    for i := 0 to aList.Count - 1 do
    begin
      if (Pos(Code, aList.Names[i]) > 0) and (Trim(aList.ValueFromIndex[i]) <> '') then
        lstCodeFilter.Items.Add(aList.Strings[i]);
    end;
  finally
    aList.Free;
  end;
end;

procedure TFrmDict.lstCodeFilterClick(Sender: TObject);
var
  i: Integer;
  sSel: string;
begin
  for i := 0 to lstCodeFilter.Count - 1 do
    if lstCodeFilter.Selected[i] then
    begin
      sSel := lstCodeFilter.Items.Strings[i];
      if Pos('=', sSel) > 0 then
      begin
        edtFieldName.Text := Copy(sSel, Pos('=', sSel) + 1, length(sSel));
      end;
    end;
end;

procedure TFrmDict.lstFieldListClick(Sender: TObject);
begin
  edtFieldCode.Text := NewAllField.Items[lstFieldList.ItemIndex].TableCode;
  edtFieldName.Text := NewAllField.Items[lstFieldList.ItemIndex].TableName;
  actSave1.Enabled := false;
  //GetFilterList(edtFieldCode.Text);
//  if edtFieldName.Text = '' then
//    SetFieldNameFromCode(edtFieldCode.Text);
end;

end.

